import Vue from 'vue'
import VueRouter from 'vue-router'
import { UserLayout, TabLayout } from '@/components/layouts'
import menuList from './config'

try {
  const originalPush = VueRouter.prototype.push
  VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
  }
} catch (e) {
  console.error(e)
}

Vue.use(VueRouter)

// 定义路由
const routes = [
  ///  user
  {
    path: '/user',
    component: UserLayout,
    redirect: '/user/login',
    hidden: true,
    children: [
      {
        path: 'login',
        name: 'Login',
        meta: { title: '登录' },
        component: () => import('@/views/user/Login')
      },
      {
        path: 'register',
        name: 'Register',
        meta: { title: '注册' },
        component: () => import('@/views/user/Register')
      },
      {
        path: 'register-result',
        name: 'RegisterResult',
        component: () => import('@/views/user/RegisterResult')
      },
      {
        path: 'alteration',
        name: 'Alteration',
        component: () => import('@/views/user/Alteration')
      }
    ]
  },

  {
    path: '/404',
    component: () => import('@/views/exception/404')
  },

  /// main routes config
  {
    path: '/',
    name: 'Dashboard',
    component: TabLayout,
    meta: { title: '首页' },
    redirect: '/dashboard/analysis',
    children: menuList
  },

  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  scrollBehavior: () => ({ y: 0 }),
  routes: routes
})

export default router
